'''
通知和日志记录功能模块
'''
from flask import g
from datetime import datetime
from config.config import OPENAI_MODEL

def create_notification(notification_type, title, message, recipient, related_task_id=None, related_execution_id=None, priority='normal', action_url=None):
    """创建系统通知"""
    conn = g.db
    cursor = conn.cursor()
    
    cursor.execute('''
    INSERT INTO notifications
    (notification_type, title, message, recipient, related_task_id, related_execution_id, priority, action_url)
    VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
    ''', (notification_type, title, message, recipient, related_task_id, related_execution_id, priority, action_url))
    
    conn.commit()

def log_ai_conversation(conn=None, related_task_id=None, related_execution_id=None, conversation_type='task_breakdown', prompt='', response='', tokens=0, model=OPENAI_MODEL, duration=0, success=True, error_message=''):
    """记录AI会话信息"""
    if conn is None:
        conn = g.db
    cursor = conn.cursor()
    
    # 根据success参数设置状态
    status = 'success' if success else 'failed'
    
    cursor.execute('''
    INSERT INTO ai_conversation_logs
    (related_task_id, related_execution_id, conversation_type, prompt, response, status, duration_seconds, token_count)
    VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
    ''', (related_task_id, related_execution_id, conversation_type, prompt, response, status, duration, tokens))
    
    conn.commit() 